package com.tma.internship.connectserver;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.tma.internship.activities.StoredFilesActivity;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public abstract class ReceiveFileFromServer extends ThreadContactServer<Integer> {
	private String fileName;
	private int page;
	private Context context;
	public ReceiveFileFromServer(List<NameValuePair> nameValuePairs,
			String action,String fileName,int page,Context context) {
		super(nameValuePairs, action);
		// TODO Auto-generated constructor stub
		this.fileName = fileName;
		this.page = page;
		this.context = context;
	}

	@Override
	public void processReceiveData(InputStream in) throws Exception {
		// TODO Auto-generated method stub
		FileOutputStream fos = context.openFileOutput(fileName+"_"+page+".png", Context.MODE_PRIVATE);
		byte[] buff = new byte[1024];//1KB
		int len;
		int sum = 0;
		while ((len=in.read(buff))>0)
		{ 
		    fos.write(buff,0,len);
		    sum += len;
		}
		fos.flush();
		fos.close();
		in.close();
		onSuccess(sum);
	}

}
